<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions"%>
<%request.setCharacterEncoding("utf-8"); %>
<% response.setContentType("text/html; charset=utf-8"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>
<style type="text/css">
	.coun_sal_find, .coun_pro_find{
		text-align: right;
	}
	.cwe_select{text-align: center;}
	
	.coun_save div{
		vertical-align: middle;
	}
	.coun_save div p{
		display: inline-block;
		vertical-align: middle;
		padding-left: 30px;
	}
	.coun_pro_info, .coun_sal_info{
		display: table;
		width: 100%;
		border: 3px solid gray;
		padding-top: 5px;
		padding-left: 5px;
		padding-right: 5px;
		margin-bottom: 10px;
	}
	.coun_pro_info div, .coun_sal_info div{
		display: table-row;
	}
	.coun_pro_info div div, .coun_sal_info div div{
		border: 1px solid gray;
		display: inline-block;
	}
	.coun_sal_info div div{width: 25%;}
	.coun_pro_info div div{width: 25%;}
	.coun_pro_info div div p, .coun_sal_info div div p{
		display: table-cell;
		height: 30px;
		vertical-align: middle;
		width: 40%;
	}
	.coun_pro_info div div p:FIRST-CHILD, .coun_sal_info div div p:FIRST-CHILD{
		background-color: #eeeeee;
		text-align: center;
		border-right: 1px solid gray;
	}
	.coun_pro_info div div p:LAST-CHILD, .coun_sal_info div div p:LAST-CHILD{
		padding: 5px;
	}
</style>
<script type="text/javascript" src="${pageContext.request.contextPath }/intranet/js/text.readonly.true.js"></script>
<script type="text/javascript">
	$(window).on('load', function () {
		$( ".cwe_select" ).selectpicker({
			style: 'btn-info',
			"width" : "150px"
		});
		readOnly();
		$( "#cs_loan" ).spinner({
		      min: 0,
		      max: 0,
		      step: 100,
		      start: 0
		      //numberFormat: "n",
		 });
		document.getElementById("salNO").focus();
	});
	var xhr;
	var rs;
	function init(){
		
		xhr = new XMLHttpRequest();
		xhr.onreadystatechange = myFunc;
				
		var btnSal = document.getElementById("btnSal");
		var btnPro = document.getElementById("btnPro");
		
		btnSal.onclick = salFunc;
		btnPro.onclick = proFunc;
		
	}
	
	function myFunc() {
		if(xhr.status == 200 && xhr.readyState==4){
			rs.innerHTML = xhr.responseText;
			readOnly();
			if(rs.id == "coun_sal_info"){
				var sa_no = document.getElementById("sal_no").value;
				if(sa_no == "") alert("검색하신 사원 정보가 없습니다!");
				//alert($("#coun_max").val()+"/"+document.getElementById("coun_max").value.replace('만원',''));
				 $( "#cs_loan" ).spinner({
				      min: 100,
				      max: document.getElementById("coun_max").value.replace('만원',''),
				      step: 100,
				      start: 100
				      //numberFormat: "C"
				 });
				 //document.getElementById("cs_loan").value = "0원";
			}
			else if(rs.id == "coun_pro_info"){
				var pd_no = document.getElementById("pro_no").value;
				if(pd_no == "") alert("검색하신 상품 정보가 없습니다!");
			}
		}
	}
	
	function salFunc(){
		var salDiv = document.getElementById("coun_sal_info");
		var salFind = document.getElementById("salNO").value.trim();
		if(salFind == ""){
			alert("사원번호를 입력해주세요!");
			document.getElementById("salNO").value = "";
			document.getElementById("salNO").focus();
			return;
		}
		var url = "${pageContext.request.contextPath}/Coun/SPSearch.clamp";
		var params="sa_no="+salFind;
		xhr.open('post',url,true);
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr.send(params);
		rs = salDiv;
		
	} 
	
	function proFunc(){
		var proDiv = document.getElementById("coun_pro_info");
		var proFind = document.getElementById("proNO").value.trim();
		if(proFind == ""){
			alert("상품번호를 입력해주세요!");
			document.getElementById("proNO").value = "";
			document.getElementById("proNO").focus();
			return;
		}
		var url = "${pageContext.request.contextPath}/Coun/SPSearch.clamp";
		var params="pd_no="+proFind;
		xhr.open('post',url,true);
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr.send(params);
		rs = proDiv;
	} 
	
	function fnSub() {
		var frm = document.getElementById("frm");
		if(frm.sal_nm.value == ""){
			alert("사원 정보를 검색 해주세요~");
			return false;
		}
		if(frm.pro_nm.value == ""){
			alert("상품 정보를 검색 해주세요~");
			return false;
		}
		if(frm.pro_yn.value == "N"){
			alert("사용하실수 없는 상품 정보 입니다!");
			return false;
		}
		if(frm.cs_content.value == ""){
			alert("상담 내용을 입력 해주세요~");
			return false;
		}
		
		document.getElementById("sa_no").value = frm.sal_no.value;
		document.getElementById("sa_dpm").value = frm.sal_dpm.value;
		document.getElementById("sa_gd").value = frm.sal_gd.value;
		document.getElementById("pd_no").value = frm.pro_no.value;
		document.getElementById("pd_rate").value = frm.pro_rate.value;
		document.getElementById("pd_pback").value = frm.pro_pback.value;
		document.getElementById("cs_max").value = frm.coun_max.value.replace("만원","");
		document.getElementById("pd_prd").value = frm.pro_prd.value;
		
		var cMax = document.getElementById("cs_max").value;
		var pMax = frm.pro_max.value.replace("만원","");
		var pMin = frm.pro_min.value.replace("만원","");
		var loan = document.getElementById("cs_loan").value;
		pMax = pMax.replace(",","");
		pMin = pMin.replace(",","");
		if(pMax == "1억원") pMax = "10000";
		cMax = Number(cMax);
		pMax = Number(pMax);
		pMin = Number(pMin);
		loan = Number(loan);
		
		if(loan == 0){
			alert("대출 신청액을 입력해주세요!");
			return false;
		}
		if(cMax<loan){
			alert("대출한도 초과 금액은 신청하실 수 없습니다!");
			return false;
		}else{
			if(pMax<loan){
				alert("상품 최대한도 초과 금액은 신청하실 수 없습니다!");
				return false;
			}
			if(pMin>loan){
				alert("상품 최저한도 미만 금액은 신청하실 수 없습니다!");
				return false;
			}
		}
		
		var flag = confirm("정말 등록 하시겠습니까?");
		if(flag){
			frm.action ="${pageContext.request.contextPath }/Coun/CounWriteOK.clamp";
			//frm.action ="${pageContext.request.contextPath }/intranet/counsel/counsel_w_forward.jsp?sa_no="+sa_no+"&pd_no="+pd_no;
			frm.submit();
			return true;
		}
		else return false;
		//${pageContext.request.contextPath }/Coun/CounWriteOK.clamp
	}
	
	function fnGoList() {
		var list = document.getElementById("list");
		list.submit();
	}
	
	function keyevent(sp) {
		if(event.keyCode == 13){
			if(sp == 's') salFunc();
			else if(sp == 'p') proFunc();
		}
	}
	
	function fnCal() {
		window.open("${pageContext.request.contextPath }/homepage/product/calculator.jsp", "_blank",
				"resizable=yes, top=100, right=200, width:700px;", true);
	}
	
</script>
</head>
<body>
	<div align="center">
		<form action="#" id="frm" name="frm" method="post">
			<input type="hidden" id="sa_no" name="sa_no" value="">
			<input type="hidden" id="pd_no" name="pd_no" value="">
			<input type="hidden" id="cs_max" name="cs_max" value="">
			<input type="hidden" id="sa_dpm" name="sa_dpm" value="">
			<input type="hidden" id="sa_gd" name="sa_gd" value="">
			<input type="hidden" id="pd_pback" name="pd_pback" value="">
			<input type="hidden" id="pd_rate" name="pd_rate" value="">
			<input type="hidden" id="pd_prd" name="pd_prd" value="">
			
			<div style="width: 1200px;">
				<h1 align="center">상담 등록<small>Counsel Write</small></h1>
				<div class="coun_sal_find">
					<p>
						<label class="label label-danger" style="font-size: 13pt;">사원번호</label>
						&nbsp;&nbsp;&nbsp;
						<input type="search" id="salNO" class="form-control input-sm" style="width: auto; display: inline-block;" value="1408M" onkeyup="keyevent('s')">
						&nbsp;&nbsp;&nbsp;
						<input type="button" value="조회" class="btn btn-success" id="btnSal"><!-- onclick="fnSalSearch()" -->  
					</p>
				</div>
				<div id="coun_sal_info" class="coun_sal_info">
					<div>
						<div>
							<p>사원번호</p>
							<p><input type="text" id="sal_no" name='sal_no'></p>
						</div>
						<div>
							<p>이 름</p>
							<p><input type="text" name='sal_nm'></p>
						</div>
						<div>
							<p>나 이</p>
							<p><input type="text"></p>
						</div>
						<div>
							<p>성 별</p>
							<p><input type="text"></p>
						</div>
					</div>
					<div>
						<div>
							<p>부 서</p>
							<p><input type="text" id="sa_dpm" name='sal_dpm'></p>
						</div>
						<div>
							<p>직 책</p>
							<p><input type="text" id="sa_gd" name='sal_gd'></p>
						</div>
						<div>
							<p>주택소유</p>
							<p><input type="text"></p>
						</div>
						<div>
							<p>차량소유</p>
							<p><input type="text"></p>
						</div>
					</div>
					<div>
						<div>
							<p>입사일</p>
							<p><input type="text"></p>
						</div>
						<div>
							<p>연 봉</p>
							<p><input type="text"></p>
						</div>
						<div>
							<p>결혼여부</p>
							<p><input type="text"></p>
						</div>
						<div>
							<p>대출한도</p>
							<p><input type="text" name='coun_max'></p>
						</div>
					</div>
				</div>
				<div class="coun_pro_find">
					<p>
						<label class="label label-danger" style="font-size: 13pt;">상품번호</label>
						&nbsp;&nbsp;&nbsp;
						<input type="search" id="proNO" class="form-control input-sm" style="width: auto; display: inline-block;" value="1408P010" onkeyup="keyevent('p')">
						&nbsp;&nbsp;&nbsp;
						<input type="button" id="btnPro" value="조회" class="btn btn-success"> <!-- onclick="fnProSearch()" -->
					</p>
				</div>
				<div id="coun_pro_info" class="coun_pro_info">
					<div>
						<div>
							<p>상품번호</p>
							<p><input type="text" id="pro_no" name='pro_no'></p>
						</div>
						<div>
							<p>상품명</p>
							<p><input type="text" name="pro_nm"></p>
						</div>
						<div>
							<p>상환방법</p>
							<p><input type="text" name="pro_pback"></p>
						</div>
						<div>
							<p>금리</p>
							<p><input type="text" name="pro_rate"></p>
						</div>
					</div>
					<div>
						<div>
							<p>상환 기간</p>
							<p><input type="text" name="pro_prd"></p>
						</div>
						<div>
							<p>최저한도</p>
							<p><input type="text" ></p>
						</div>
						<div>
							<p>최대한도</p>
							<p><input type="text" ></p>
						</div>
						<div>
							<p>상품 상태</p>
							<p><input type="text" ></p>
						</div>
					</div>
				</div>
				
				<div class="coun_save">
					<div style="display: inline-block;">
						<p><label class="label label-danger" style="font-size: 13pt;">비 고</label></p>
						<p><textarea id="cs_content" name="cs_content" rows="3" cols="95" style="resize: none;"></textarea></p>
					</div>
					<div style="display: inline-block;">
						<p><label class="label label-danger" style="font-size: 13pt;">신청액 (단위:만원)</label></p>
						<p>
							<input type="number" id="cs_loan" name="cs_loan" value="0" readonly="readonly">
						</p>
					</div>
				</div>
				<div align="right">
					<p>
						<input type="button" value="계산기" class="btn btn-primary" onclick="fnCal()">
						&nbsp;&nbsp;&nbsp;
						<input type="button" value="상담등록" class="btn btn-primary" onclick="return fnSub()">
						&nbsp;&nbsp;&nbsp;
						<input type="button" value="목록" class="btn btn-primary" onclick="fnGoList()">
					</p>
				</div>
			</div>
		</form>
		<form action="${pageContext.request.contextPath}/Coun/CounList.clamp" method="post" id="list" name="list">
			<input type="hidden" name="list" value="list">
		</form>
	</div>
	<script>init();</script>
</body>
</html>